<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input id="txt" type="text" value="">
    <input id="btn" type="button" value="七位">
    <input id="btns" type="button" value="转换16进制">
    <script>
        var $random = document.getElementsByTagName('input');
        // text
        $random[0].style.width = '100px';
        $random[0].style.height = '15px';
        //button
        $random[1].style.background = 'cyan';
        $random[1].style.border = 'none';
        $random[1].style.height = '20px';

        btn.onclick = function () {
            // 生成随机7个 0--9  的验证码
            btn.value = ' '+ randomNum(0,16) + randomNum(0, 16) + randomNum(0, 16) + randomNum(0, 16) + randomNum(0, 16) + randomNum(0, 16)
            txt.value = btn.value
            // var num = Number($random).toString(16)
            btns.onclick = function(){
                var num = Number(txt.value).toString(16)
                txt.value = '#'+num;
                
            }
            
        }
        // 先判断两个参数的大小
        function randomNum(n, m){
            var max, min;
            max = n > m ? n : m;
            min = n < m ? n : m;

            return parseInt(Math.random() * (max - min + 1) + min);
        }
        // randomNum(0,100)
    </script>
</body>

</html>